/**
 * 
 */
package gui;

import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.rmi.RemoteException;
import java.util.Calendar;

import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTextField;
import javax.swing.JToolBar;

import mochito.dataClasses.musicItem.MusicItem;
import mochito.serverClasses.ServerManager;

/**
 * @author bin04017
 * 
 */
public class MochitoServerGui extends JFrame implements ActionListener {

	private static final long serialVersionUID = 1L;

	private JPanel jContentPane = null;

	private JSplitPane jSplitPane = null;

	private JPanel jPanel = null;

	private JPanel jPanel1 = null;

	private JScrollPane jScrollPane = null;

	private JList jList = null;

	private JButton jButton = null;

	private JButton jButton1 = null;

	private JPanel jPanel2 = null;

	private JToolBar jToolBar = null;

	private JLabel jLabel = null;

	private JLabel jLabel1 = null;

	private JTextField jTextField = null;

	private JTextField jTextField1 = null;

	private JButton jButton2 = null;

	private DefaultListModel defaultListModel = null;

	private ServerManager serverManager = null;

	private JLabel jLabel2 = null;

	private JLabel jLabel3 = null;

	private JTextField jTextField2 = null;

	private JTextField jTextField3 = null;

	/**
	 * This is the default constructor
	 */
	public MochitoServerGui(String host) {
		super();
		this.serverManager = new ServerManager(host);
		initialize();
		this.serverManager.run();
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setSize(471, 324);
		this.setContentPane(getJContentPane());
		this.setTitle("MochitoServer");
		this.addWindowListener(new java.awt.event.WindowAdapter() {
			public void windowClosing(java.awt.event.WindowEvent e) {
				System.exit(0);
			}
		});
	}

	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			jContentPane = new JPanel();
			jContentPane.setLayout(new BorderLayout());
			jContentPane.add(getJSplitPane(), BorderLayout.CENTER);
		}
		return jContentPane;
	}

	/**
	 * This method initializes jSplitPane
	 * 
	 * @return javax.swing.JSplitPane
	 */
	private JSplitPane getJSplitPane() {
		if (jSplitPane == null) {
			jSplitPane = new JSplitPane();
			jSplitPane.setLeftComponent(getJPanel());
			jSplitPane.setRightComponent(getJPanel1());
		}
		return jSplitPane;
	}

	/**
	 * This method initializes jPanel
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJPanel() {
		if (jPanel == null) {
			jPanel = new JPanel();
			jPanel.setLayout(new BorderLayout());
			jPanel.add(getJToolBar(), BorderLayout.NORTH);
			jPanel.add(getJPanel2(), BorderLayout.CENTER);
		}
		return jPanel;
	}

	/**
	 * This method initializes jPanel1
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJPanel1() {
		if (jPanel1 == null) {
			jPanel1 = new JPanel();
			jPanel1.setLayout(new BorderLayout());
			jPanel1.add(getJScrollPane(), BorderLayout.CENTER);
		}
		return jPanel1;
	}

	/**
	 * This method initializes jScrollPane
	 * 
	 * @return javax.swing.JScrollPane
	 */
	private JScrollPane getJScrollPane() {
		if (jScrollPane == null) {
			jScrollPane = new JScrollPane();
			jScrollPane.setViewportView(getJList());
		}
		return jScrollPane;
	}

	/**
	 * This method initializes jList
	 * 
	 * @return javax.swing.JList
	 */
	private JList getJList() {
		if (jList == null) {
			jList = new JList(this.getDefaultListModel());
		}
		return jList;
	}

	private DefaultListModel getDefaultListModel() {
		if (defaultListModel == null) {
			defaultListModel = new DefaultListModel();
		}
		return defaultListModel;
	}

	/**
	 * This method initializes jButton
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getJButton() {
		if (jButton == null) {
			jButton = new JButton();
			jButton.setText("New");
			jButton.addActionListener(this);
		}
		return jButton;
	}

	/**
	 * This method initializes jButton1
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getJButton1() {
		if (jButton1 == null) {
			jButton1 = new JButton();
			jButton1.setText("Delete");
			jButton1.addActionListener(this);
		}
		return jButton1;
	}

	/**
	 * This method initializes jPanel2
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJPanel2() {
		if (jPanel2 == null) {
			GridBagConstraints gridBagConstraints4 = new GridBagConstraints();
			gridBagConstraints4.fill = GridBagConstraints.BOTH;
			gridBagConstraints4.gridy = 4;
			gridBagConstraints4.weightx = 1.0;
			gridBagConstraints4.gridx = 1;
			GridBagConstraints gridBagConstraints31 = new GridBagConstraints();
			gridBagConstraints31.fill = GridBagConstraints.BOTH;
			gridBagConstraints31.gridy = 3;
			gridBagConstraints31.weightx = 1.0;
			gridBagConstraints31.gridx = 1;
			GridBagConstraints gridBagConstraints21 = new GridBagConstraints();
			gridBagConstraints21.gridx = 0;
			gridBagConstraints21.gridy = 4;
			jLabel3 = new JLabel();
			jLabel3.setText("Start Date");
			GridBagConstraints gridBagConstraints11 = new GridBagConstraints();
			gridBagConstraints11.gridx = 0;
			gridBagConstraints11.gridy = 3;
			jLabel2 = new JLabel();
			jLabel2.setText("Start Time");
			GridBagConstraints gridBagConstraints3 = new GridBagConstraints();
			gridBagConstraints3.gridx = 1;
			gridBagConstraints3.gridy = 2;
			GridBagConstraints gridBagConstraints2 = new GridBagConstraints();
			gridBagConstraints2.fill = GridBagConstraints.BOTH;
			gridBagConstraints2.gridy = 1;
			gridBagConstraints2.weightx = 1.0;
			gridBagConstraints2.gridx = 1;
			GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
			gridBagConstraints1.fill = GridBagConstraints.BOTH;
			gridBagConstraints1.gridy = 0;
			gridBagConstraints1.weightx = 1.0;
			gridBagConstraints1.gridx = 1;
			GridBagConstraints gridBagConstraints = new GridBagConstraints();
			gridBagConstraints.gridx = 0;
			gridBagConstraints.gridy = 1;
			jLabel1 = new JLabel();
			jLabel1.setText("Filepath");
			jLabel = new JLabel();
			jLabel.setText("Name:");
			jPanel2 = new JPanel();
			jPanel2.setLayout(new GridBagLayout());
			jPanel2.add(jLabel, new GridBagConstraints());
			jPanel2.add(jLabel1, gridBagConstraints);
			jPanel2.add(getJTextField(), gridBagConstraints1);
			jPanel2.add(getJTextField1(), gridBagConstraints2);
			jPanel2.add(getJButton2(), gridBagConstraints3);
			jPanel2.add(jLabel2, gridBagConstraints11);
			jPanel2.add(jLabel3, gridBagConstraints21);
			jPanel2.add(getJTextField2(), gridBagConstraints31);
			jPanel2.add(getJTextField3(), gridBagConstraints4);
		}
		return jPanel2;
	}

	/**
	 * This method initializes jToolBar
	 * 
	 * @return javax.swing.JToolBar
	 */
	private JToolBar getJToolBar() {
		if (jToolBar == null) {
			jToolBar = new JToolBar();
			jToolBar.add(getJButton());
			jToolBar.add(getJButton1());
		}
		return jToolBar;
	}

	/**
	 * This method initializes jTextField
	 * 
	 * @return javax.swing.JTextField
	 */
	private JTextField getJTextField() {
		if (jTextField == null) {
			jTextField = new JTextField();
			jTextField.setText("Enter Name");
			jTextField.setEditable(false);
		}
		return jTextField;
	}

	/**
	 * This method initializes jTextField1
	 * 
	 * @return javax.swing.JTextField
	 */
	private JTextField getJTextField1() {
		if (jTextField1 == null) {
			jTextField1 = new JTextField();
			jTextField1.setText("Set Path");
			jTextField1.setEditable(false);
		}
		return jTextField1;
	}

	/**
	 * This method initializes jButton2
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getJButton2() {
		if (jButton2 == null) {
			jButton2 = new JButton();
			jButton2.setText("Browse");
			jButton2.setEnabled(false);
			jButton2.addActionListener(this);
		}
		return jButton2;
	}

	private void newEnable(Boolean status) {
		this.jTextField.setEditable(status);
		this.jTextField1.setEditable(status);
		this.jTextField2.setEditable(status);
		this.jTextField3.setEditable(status);
		this.jButton2.setEnabled(status);
		if (status) {
			this.jButton.setText("Cancel");
			this.jButton1.setText("Commit");
		} else {
			this.jButton.setText("New");
			this.jButton1.setText("Delete");
			this.jTextField.setText("Enter Name");
			this.jTextField1.setText("Set Path");
		}
	}

	/**
	 * 
	 */
	public void actionPerformed(ActionEvent e) {
		if (e.getSource().equals(jButton)) {
			if (jButton.getText().equalsIgnoreCase("New")) {
				this.newEnable(true);
			} else if (jButton.getText().equalsIgnoreCase("Cancel")) {
				this.newEnable(false);
			}
		} else if (e.getSource().equals(jButton2)) {
			JFileChooser fc = new JFileChooser();
			if (fc.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
				this.jTextField1
						.setText(fc.getSelectedFile().getAbsolutePath());
				if (this.jTextField.getText().equalsIgnoreCase("Enter Name")) {
					this.jTextField.setText(this.jTextField1.getText());
				}
			}
		} else if (e.getSource().equals(jButton1)) {
			if (jButton1.getText().equalsIgnoreCase("Delete")) {
				if (!this.getJList().isSelectionEmpty()) {
					String item = this.getJList().getSelectedValue().toString();
					delete(item);
				}
			} else if (jButton1.getText().equalsIgnoreCase("Commit")) {
				insert(this.jTextField.getText(), this.jTextField1.getText());
				this.newEnable(false);
			}
		}

		this.refresh();
	}

	private void delete(String stringItem) {
		MusicItem item;
		try {
			String[] itemAttr = stringItem.split(",");
			item = new MusicItem(itemAttr[0], itemAttr[1]);
			item.setStartTime(itemAttr[2]);
			item.setStartDate(itemAttr[3]);
			this.serverManager.removeMusicItem(item);
		} catch (Exception e) {
			System.err.println(e.toString());
		}
	}

	private void insert(String name, String fileName) {
		try {
			MusicItem item = new MusicItem(name, fileName);
			item.setStartTime(this.jTextField2.getText());
			item.setStartDate(this.jTextField3.getText());
			this.serverManager.addMusicItem(item);
		} catch (RemoteException e) {
			e.printStackTrace();
		}
	}

	private void refresh() {
		try {
			this.getDefaultListModel().clear();
			for (String item : this.serverManager.getMusicItemsNames()) {
				if (!this.getDefaultListModel().contains(
						item
								+ ", "
								+ this.serverManager.getMusicItemFile(item)
								+ ","
								+ this.serverManager
										.getMusicItemStartTime(item)
								+ ", "
								+ this.serverManager
										.getMusicItemStartDate(item))) {
					this.getDefaultListModel().addElement(
							item
									+ ", "
									+ this.serverManager.getMusicItemFile(item)
									+ ", "
									+ this.serverManager
											.getMusicItemStartTime(item)
									+ ", "
									+ this.serverManager
											.getMusicItemStartDate(item));
				}
			}
		} catch (RemoteException e) {
			e.printStackTrace();
		}
	}

	/**
	 * This method initializes jTextField2
	 * 
	 * @return javax.swing.JTextField
	 */
	private JTextField getJTextField2() {
		if (jTextField2 == null) {
			jTextField2 = new JTextField();
			Calendar cal = Calendar.getInstance();
			Integer hour = cal.get(Calendar.HOUR_OF_DAY);
			Integer min = cal.get(Calendar.MINUTE);
			Integer sec = cal.get(Calendar.SECOND);
			jTextField2.setText(hour + ":" + min + ":" + sec);
			jTextField2.setEditable(false);
		}
		return jTextField2;
	}

	/**
	 * This method initializes jTextField3
	 * 
	 * @return javax.swing.JTextField
	 */
	private JTextField getJTextField3() {
		if (jTextField3 == null) {
			jTextField3 = new JTextField();
			Calendar cal = Calendar.getInstance();
			Integer year = cal.get(Calendar.YEAR);
			Integer mon = cal.get(Calendar.MONTH) + 1;
			Integer day = cal.get(Calendar.DAY_OF_MONTH);
			jTextField3.setText(year + "/" + mon + "/" + day);
			jTextField3.setEditable(false);
		}
		return jTextField3;
	}
} // @jve:decl-index=0:visual-constraint="10,10"
